package com.itcast.controller;

import com.itcast.entity.Student;
import com.itcast.service.StudentService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class AjaxStudentController {

    @Autowired
    private StudentService studentService;

    // 分页查询学生信息
    // http://localhost:8100/student/list?pageNum=1&pageSize=5
    // http://localhost:8100/student/list?pageNum=1&pageSize=5&name=张
    @GetMapping("/list")
    public PageInfo<Student> getStudentsByPage(
            Student student,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        return studentService.getStudentsByPage(student, pageNum, pageSize);
    }

    // 查询总数 http://localhost:8100/student/count
    @GetMapping("/count")
    public int count(Student student) {
        return studentService.count(student);
    }

    // 根据ID查询学生 http://localhost:8100//student/1
    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable Integer id) {
        return studentService.getStudentById(id);
    }

    // 新增学生 http://localhost:8100/student
    @PostMapping
    public int addStudent(@RequestBody Student student) {
        return studentService.addStudent(student);
    }

    // 修改学生信息 http://localhost:8100/student
    @PutMapping
    public int updateStudent(@RequestBody Student student) {
        return studentService.updateStudent(student);
    }

    // 删除学生 http://localhost:8100//student/1
    @DeleteMapping("/{id}")
    public int deleteStudent(@PathVariable Integer id) {
        return studentService.deleteStudent(id);
    }
}